// index.js
const http = require('../../utils/http')
const api = require('../../utils/api')
const util = require('../../utils/util')
const ui = require('../../utils/ui')

import Dialog from "../../components/dialog/dialog"

// 获取应用实例
const app = getApp()

Page({

  onLoad() {
    
  },

  onShow() {
    this.setData({
      telState: util.nonNull(getApp().globalData.tel)
    })
    this.getData()
  },

  data: {
    telState: false, //是否记录电话
    userInfo: {},
    storeInfo: {},
    giftInfo: {},
    hasUserInfo: false,
  },

  getData() {
    http.request({
      url: api.userInfo,
      data: {
        memberId: getApp().globalData.memberId
      }
    }).then(res=> {
      console.log(res)
      this.setData({
        userInfo: res.data.result
      })
    })
    http.request({
      url: api.storeInfo,
      data: {
        storeId: getApp().globalData.selectStoreId
      }
    }).then(res=> {
      console.log(res)
      this.setData({
        storeInfo: res.data.result
      })
    })
    http.request({
      url: api.timeGift,
      data: {
        memberId: getApp().globalData.memberId
      }
    }).then(res=> {
      console.log(res)
      this.setData({
        giftInfo: res.data.result
      })
    })
  },

  editProfile() {
    wx.navigateTo({
      url: '/pages/profile_edit/profile_edit'+util.paramsWrap(this.data.userInfo),
    })
  },
  
  contactCompany(event) {
    let phone = event.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },

  showWifiDialog() {
    Dialog.confirm({
      selector: "#wifi-dialog",
      title: "店铺WIFI",
      cancelButtonText: "取消",
      confirmButtonText: "一键连接",
      confirmCallback:() => {
        util.wifiConnect({
          ssid: this.storeInfo.wifi,
          password: this.storeInfo.password
        })
      },
      cancelCallback:() => {
        // wx.setClipboardData({
        //   data: this.data.storeInfo?.password ?? "",
        //   success: function (res) {
        //     ui.showToast({
        //       title: '复制成功'
        //     })
        //   }
        // })
      }
    });
  },

  purchase() {
    if (this.toLogin()) {
      return
    }
    wx.navigateTo({
      url: '/pages/purchase/purchase',
    })
  },

  techSup() {
    wx.navigateTo({
      url: '/pages/tech_support/tech_support',
    })
  },

  toLogin() {
    if (!this.data.telState) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
    return !this.data.telState
  },

})